package define

import (
	"gopkg.in/yaml.v2"
	"log"
	"os"
)

var Conf Config

const (
	ChunkSize     = 5 * (1 << 20)
	ChunkBasePath = "/tmp/clouddisk/chunk/"
	FileBasePath  = "/tmp/clouddisk/"
)

// TokenExpire one month
type Config struct {
	Token    Token    `yaml:"Token"`
	Captcha  Captcha  `yaml:"Captcha"`
	Date     Date     `yaml:"Date"`
	Redis    Redis    `yaml:"Redis"`
	MySQL    MySQL    `yaml:"MySQL"`
	OSS      OSS      `yaml:"OSS"`
	RabbitMQ RabbitMQ `yaml:"RabbitMQ"`
}

type Token struct {
	Key string `yaml:"key"`
}

type Captcha struct {
	Expire int `yaml:"expire"`
}

type Date struct {
	Format string `yaml:"format"`
}

type Redis struct {
	Host     string `yaml:"host"`
	Port     string `yaml:"port"`
	Password string `yaml:"password"`
}
type MySQL struct {
	Host     string `yaml:"host"`
	Port     string `yaml:"port"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	Url      string `yaml:"url"`
	DBName   string `yaml:"dbname"`
}
type OSS struct {
	Endpoint        string `yaml:"endpoint"`
	AccessKey       string `yaml:"accessKey"`
	AccessKeySecret string `yaml:"accessKeySecret"`
	Bucket          string `yaml:"bucket"`
	ChunkSize       int    `yaml:"chunkSize"`
}

type RabbitMQ struct {
	AsyncTransferEnable   bool   `yaml:"asyncTransferEnable"`
	Url                   string `yaml:"url"`
	TransferExchangeName  string `yaml:"transferExchangeName"`
	TransferOssQueueName  string `yaml:"transferOssQueueName"`
	TransOssErrQueueName  string `yaml:"transOssErrQueueName"`
	TransOssRoutingKey    string `yaml:"transOssRoutingKey"`
	TransOssErrRoutingKey string `yaml:"transOssErrRoutingKey"`

	RemoveExchangeName    string `yaml:"removeExchangeName"`
	RemoveChunkQueueName  string `yaml:"removeChunkQueueName"`
	RemoveChunkRoutingKey string `yaml:"removeChunkRoutingKey"`
}

func LoadConfig(path string) error {
	conf := Config{}
	f, err := os.Open(path)
	if err != nil {
		log.Println(err)
		return err
	}
	err = yaml.NewDecoder(f).Decode(&conf)
	if err != nil {
		log.Println(err)
		return err
	}
	Conf = conf
	return nil
}
